package cn.hallele.bbts.core.module.core.config;

import cn.hallele.bbts.core.module.core.encode.RSAPasswordEncode;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import jakarta.annotation.PostConstruct;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.crypto.password.PasswordEncoder;

/**
 * CoreConfig
 *
 * @author anle5
 * @since 2023/12/5 星期二 23:31
 */
@Slf4j
@Configuration
@EnableConfigurationProperties(CoreProperties.class)
public class CoreConfig {
    @Autowired
    private CoreProperties coreProperties;

    @Autowired
    private ObjectMapper objectMapper;

    /**
     * init
     */
    @PostConstruct
    public void init() {
        objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
        objectMapper.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
    }

    /**
     * 系统统一的加解密工具
     *
     * @return {@link RSAPasswordEncode}
     */
    @Bean
    public PasswordEncoder passwordEncoder() {
        CoreProperties.PasswordEncode passwordEncode = coreProperties.getPasswordEncode();
        return new RSAPasswordEncode(passwordEncode.getPubKey(), passwordEncode.getPriKey());
    }
}
